package com.zhy.sping.listener;

import com.aliyun.oss.event.ProgressEvent;
import com.aliyun.oss.event.ProgressEventType;
import com.aliyun.oss.event.ProgressListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class OssUploadProgressListener implements ProgressListener {
    private Logger logger= LoggerFactory.getLogger(OssUploadProgressListener.class);
    private long bytesWritten = 0;
    private long totalBytes = -1;
    private boolean succeed = false;//上传是否成功的标记


    @Override
    public void progressChanged(ProgressEvent progressEvent) {
        long bytes = progressEvent.getBytes();
        ProgressEventType eventType = progressEvent.getEventType();
        switch (eventType) {
            case TRANSFER_STARTED_EVENT:
               logger.info("开始上传……");
                break;
            case REQUEST_CONTENT_LENGTH_EVENT:
                this.totalBytes = bytes;
               logger.info("本次一共上传："+this.totalBytes);
                break;
            case REQUEST_BYTE_TRANSFER_EVENT:
                this.bytesWritten += bytes;
                if (this.totalBytes != -1) {
                    int percent = (int)(this.bytesWritten * 100.0 / this.totalBytes);
                    logger.info("目前上传了："+bytes + "字节,当前的上传进度为： " + percent + "%(" + this.bytesWritten + "/" + this.totalBytes + ")");
                } else {
                   logger.info(bytes + " bytes have been written at this time, upload ratio: unknown" + "(" + this.bytesWritten + "/...)");
                }
                break;
            case TRANSFER_COMPLETED_EVENT:
                this.succeed = true;
               logger.info("上传完成, 一共上传了：" + this.bytesWritten + "字节");
                break;
            case TRANSFER_FAILED_EVENT:
                System.out.println("上传异常");
                break;
            default:
                break;
        }


    }


    public  boolean isSucceed(){
        return succeed;
    }



}
